<template>
    <div class="category_main">
        <div class="head">
            <van-nav-bar
            title="标题"
            left-text="返回"
            left-arrow
            @click-left="onClickLeft"
            />
        </div>
        <div class="contentBox">
            <div class="categoryList">
                <van-tabs @change="onChange" v-model="active">
                    <van-tab v-for="item in navData" :key="item.id" :title="item.name"  />
                </van-tabs>
               <div class="goodslist">
                    <div class="info">
                        <p>{{this.currentNav.name}}</p>
                        <p>{{this.currentNav.front_desc}}</p>
                    </div>
                    <van-grid :column-num="2" :gutter="5">
                        <van-grid-item  v-for=" item in goodslist" :key="item.id" :to="`/cart/goods?id=${item.id}`">
                            <van-image :src="item.list_pic_url"  class="img"/>
                            <p class="name">{{item.name}}</p>
                            <p class="price">￥{{item.retail_price}}</p>
                        </van-grid-item>
                    </van-grid>
               </div>
            </div>
        </div>
    </div>
</template>

<script>
    import { categoryNav,goodsList} from "@/api/category/categorylist"
    import Vue from 'vue';
    import { Toast } from 'vant';

    Vue.use(Toast);
    export default {
        data() {
            return {
                id:'',
                active:'',
                currentNav:{},
                navData:[],
                goodslist:[]
            }
        },
        created(){
            this.id=this.$route.query.id;
            categoryNav({
                id:this.id
            })
            .then((res) => {
                // console.log(res);
                this.currentNav=res.currentNav;
                this.navData=res.navData;
                this.active=this.navData.findIndex(el=>el.id==this.id)
            })

            goodsList({
                categoryId:this.id
            })
            .then((res) => {
                this.goodslist=res.data
            })
            
        },
        methods: {
            onClickLeft(){
                // console.log(this.$route.to);
                this.$router.go(-1)
            },
            onChange(index){
                this.id=this.navData[index].id;
                this.currentNav=this.navData[index];

                goodsList({
                    categoryId:this.id
                })
                .then((res) => {
                    this.goodslist=res.data
                    if(this.goodslist.length==0){
                        Toast('数据库暂无内容');
                    }
                })

                
            }
        },
        mounted () {
            this.$router.afterEach((to, from, next) => {
                window.scrollTo(0, 0)
            })
        }  
    }
</script>

<style lang="scss" scoped>

@mixin guid-sty{

    .img{
        width: 150px;
        height: 150px;
        margin: 0 auto;
    }

    .name{
        margin: 7.5px 12px 0;
        text-align: center;
        padding: 0 10px;
        font-size: 12px;
    }
    .price{
        text-align: center;
        font-size: 15px;
        color: #b4282d;
        margin-bottom: 0;
    }
}
.head {
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 9;
}
.contentBox{
    background-color: #eee;
    margin-top: 50px;

    .categoryList{

        .goodslist{

            @include guid-sty;

            .info{
                height: 100px;
                text-align: center;
                background: #fff;
                padding: 15px;
                margin-top: 10px;
                margin-bottom: 2.5px;
                box-sizing: border-box;

                p:nth-child(1){
                    color: #333;
                    font-size: 15px;
                    margin-bottom: 16px;
                    margin-top: 7px;
                    font-weight: 600;
                }
                
                p:nth-child(2){
                    margin: 0;
                }
            }
        }
    }
}
</style>